/*
 * Created by JFormDesigner on Thu Jun 27 08:17:26 CST 2024
 */

package scct.chenlixian.ui;

import scct.chenlixian.dao.UserDao;
import scct.chenlixian.model.USer;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.util.List;

/**
 * @author DELL
 */
public class PanelUserManager extends JPanel {
    DefaultTableModel model;
    FormMain main;
    public PanelUserManager(FormMain main) {
        this.main=main;
        initComponents();
        initView();//自定义的初始化方法
    }
    private void initView() {
//        自己的初始化
//        获取表格对象的数据模型，通过对模型的操作，可以为表格添加和现实数据
        model = (DefaultTableModel) tabUser.getModel();
        model.addColumn("id");
        model.addColumn("用户名");
        model.addColumn("密码");
        model.addColumn("性别");
        model.addColumn("年龄");
        model.addColumn("手机号码");
        getTable();
    }
    private void getTable()
    {
        //删除原有数据
        model.setRowCount(0);
        UserDao userDao=new UserDao();
        List<USer> allUser = userDao.getAllUser();
        for(USer user:allUser )
        {
            model.addRow(new Object[]{user.getId(),user.getUsername(),user.getPassword(),user.getGender(),
                    user.getAge(),user.getMobile()});
        }
    }

    private void btnAdd(ActionEvent e) {
        DlgUserAdd dlgUserAdd=new DlgUserAdd(main);
//设置为模态对话框
        dlgUserAdd.setModal(true);
        dlgUserAdd.setVisible(true);
        getTable();
        
    }

    private void btnUpdate(ActionEvent e) {
        //判断用户是否选择表中的一行
        if (tabUser.getSelectedRowCount()==0)
        {
            JOptionPane.showMessageDialog(this,"请选择要修改的用户");
            return;
        }
        int index=tabUser.getSelectedRow();
        //System.out.println(index);
        int id=Integer.parseInt(model.getValueAt(index,0).toString());
        //System.out.println(id);
        DlgUserUpdate dlgUserUpdate= new DlgUserUpdate(main,id);
        dlgUserUpdate.setModal(true);
        dlgUserUpdate.setVisible(true);
        getTable();
    }



    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        // Generated using JFormDesigner Evaluation license - CLX
        PanelMain = new JPanel();
        btnAdd = new JButton();
        btnUpdate = new JButton();
        scrollPane1 = new JScrollPane();
        tabUser = new JTable();

        //======== this ========
        setBorder ( new javax . swing. border .CompoundBorder ( new javax . swing. border .TitledBorder ( new javax . swing. border .
        EmptyBorder ( 0, 0 ,0 , 0) ,  "JF\u006frmD\u0065sig\u006eer \u0045val\u0075ati\u006fn" , javax. swing .border . TitledBorder. CENTER ,javax . swing
        . border .TitledBorder . BOTTOM, new java. awt .Font ( "Dia\u006cog", java .awt . Font. BOLD ,12 ) ,
        java . awt. Color .red ) , getBorder () ) );  addPropertyChangeListener( new java. beans .PropertyChangeListener ( )
        { @Override public void propertyChange (java . beans. PropertyChangeEvent e) { if( "\u0062ord\u0065r" .equals ( e. getPropertyName () ) )
        throw new RuntimeException( ) ;} } );
        setLayout(new BorderLayout());

        //======== PanelMain ========
        {
            PanelMain.setLayout(new FlowLayout(FlowLayout.RIGHT));

            //---- btnAdd ----
            btnAdd.setText("\u65b0\u589e");
            btnAdd.addActionListener(e -> btnAdd(e));
            PanelMain.add(btnAdd);

            //---- btnUpdate ----
            btnUpdate.setText("\u4fee\u6539");
            btnUpdate.addActionListener(e -> btnUpdate(e));
            PanelMain.add(btnUpdate);
        }
        add(PanelMain, BorderLayout.NORTH);

        //======== scrollPane1 ========
        {

            //---- tabUser ----
            tabUser.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
            scrollPane1.setViewportView(tabUser);
        }
        add(scrollPane1, BorderLayout.CENTER);
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    // Generated using JFormDesigner Evaluation license - CLX
    private JPanel PanelMain;
    private JButton btnAdd;
    private JButton btnUpdate;
    private JScrollPane scrollPane1;
    private JTable tabUser;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
